package com.ys.web.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ys.extend.utils.YSBeanUtil;
import com.ys.web.dao.CommonDao;
import com.ys.web.entity.Industry;
import com.ys.web.entity.Quadrant;
import com.ys.web.pojo.IndustryMsgInfo;
import com.ys.web.remotemodel.RemoteIndustryInfo;
import com.ys.web.service.IndustryService;
import com.ys.web.ws.service.ManageIndustryService;

/**
 * 行业以及对应的四象限信息-服务
 * @author wdj
 *
 */
@Service
@Transactional
public class IndustryServiceImpl implements IndustryService{

	@Autowired
	private CommonDao commonDao;
	@Resource
	private ManageIndustryService manageIndustryService;
		
	/**
	 * 获取全部行业信息
	 * @return 全部行业信息列表
	 */
	@Override
	public List<RemoteIndustryInfo> getALLIndustryList() {
		return manageIndustryService.getALLIndustryList();
	}

	/**
	 * 获取全部行业以及对应的四象限信息
	 * @param infopage 当前页码
	 * @return 全部行业
	 */
	@Override
	public List<IndustryMsgInfo> getALLIndustryByPage(int currentPage) {
		List<IndustryMsgInfo> list = new ArrayList<>();

		List<RemoteIndustryInfo> remoteIndustryInfoList = manageIndustryService.getALLIndustryList();
		for (RemoteIndustryInfo industry : remoteIndustryInfoList) {
			IndustryMsgInfo industryMsgInfo = new IndustryMsgInfo();
			industryMsgInfo.setIndustryId(industry.getId());
			industryMsgInfo.setIndustryName(industry.getIndustry());
			
			list.add(industryMsgInfo);
		}
		return list;
	}

	
		
	/**
	 * 添加或者修改行业对应的四象限信息
	 * @param industryQuadrantInfo
	 */
	@Override
	public void addOrUpdateQuadrantInfo(IndustryMsgInfo industryQuadrantInfo) {
//		String industryId = industryQuadrantInfo.getIndustryId();
//		if(industryId == null) {
//			Industry industry = new Industry();
//			industry.setIndustryName(industryQuadrantInfo.getIndustryName());
//			//保存行业
//			Integer id = (Integer) commonDao.save(industry);			
//			//保存对应四象限
//			for (Quadrant quadrantInfo : industryQuadrantInfo.getQuadrantList()) {					
//				quadrantInfo.setIndustryId(id);				
//				commonDao.save(quadrantInfo);
//			}			
//		}
//		else {
//			Industry industry = commonDao.getEntity(Industry.class, industryId);
//			industry.setIndustryName(industryQuadrantInfo.getIndustryName());
//			//修改行业	
//			commonDao.saveOrUpdate(industry);
		
			//修改或者添加对应的四象限信息
			for (Quadrant quadrantInfo : industryQuadrantInfo.getQuadrantList()) {				
				Quadrant quadrant = new Quadrant();
				try {
					YSBeanUtil.copyBeanNotNull2BeanExactly(quadrantInfo, quadrant);
					if(quadrantInfo.getId()!=null) {
						quadrant.setId(quadrantInfo.getId());
					}
					quadrant.setIndustryId(industryQuadrantInfo.getIndustryId());
					
					commonDao.saveOrUpdate(quadrant);			
				} catch (Exception e) {
					e.printStackTrace();
				}
			}	
			
//		}
		
	}
	
	/**
	 * 删除行业以及对应的四象限信息
	 * @param industryId 行业id
	 */
	@Override
	public void deleteIndustryQuadrantInfo(Integer industryId) {
		//删除行业
		Industry industry = commonDao.get(Industry.class, industryId);
		commonDao.delete(industry);		
		
		//删除对应的四象限信息
		List<Quadrant> list = commonDao.findByProperty(Quadrant.class, "industryId", industryId);
		for (Quadrant quadrant : list) {
			commonDao.delete(quadrant);			
		}
	}
	
	/**
	 * 判断是否存在相同行业
	 * @param industryId 行业id
	 * @param industryName 行业名称
	 * @return true：不存在相同行业；false：存在相同行业
	 */
	@Override
	public Boolean ifExistIndustry(Integer industryId, String industryName) {		
		industryId = industryId==null ? 0 : industryId;
		String hql = " from Industry where id <> ? and industryName = ?";
		List<Industry> list = commonDao.findHql(hql, industryId, industryName);
		
		if(list!=null && list.size()>0) {
			return false;
		}
		else {
			return true;
		}
	}

	@Override
	public List<Industry> getIndustryByIsSynchronizedToYsb(String isIndustryToYsbSynchronized) {
		List<Industry> industrys = commonDao.findByProperty(Industry.class, "isIndustryToYsbSynchronized", isIndustryToYsbSynchronized);
		return industrys;
	}

	@Override
	public Industry getIndustryByIndustryName(String industryName) {
		Industry industry = commonDao.findUniqueByProperty(Industry.class, "industryName", industryName);
		return industry;
	}

	@Override
	public void saveOrUpdate(Industry industry) {
		// TODO Auto-generated method stub
		commonDao.saveOrUpdate(industry);
		
	}

}
